package org.apak.control;

public class Validator {
	public ErrorList findErrors(DeviceForm device) {
		ErrorList errors = new ErrorList(device);
		if(device.getName().length() < 1 || device.getName().length() > 20)
			errors.addError("name", "Name error");
		if(device.getReg_no().length() > 99)
			errors.addError("reg_no", "Wrong serial number");
		return errors;
	}
	
	public ErrorList findErrors(ServiceActionForm serviceAction) {
		ErrorList errors = new ErrorList(serviceAction);
		if(serviceAction.getAction_description().length() < 1)
			errors.addError("action_description", "Description cannot be empty");
		if(getValidInt(serviceAction.getService_amount()) == null)
			errors.addError("service_amount", "Wrong amount");
		if(getValidInt(serviceAction.getPrice()) == null)
			errors.addError("price", "Wrong price");
		return errors;
	}
	
	public ErrorList findErrors(ServicePartForm servicePart) {
		ErrorList errors = new ErrorList(servicePart);
		if(servicePart.getPart_name().length() < 1)
			errors.addError("part_name", "Name cannot be empty");
		if(getValidInt(servicePart.getPart_count()) == null)
			errors.addError("part_count", "Wrong amount");
		if(getValidInt(servicePart.getPart_price()) == null)
			errors.addError("part_price", "Wrong price");
		return errors;
	}
	
	public ErrorList findErrors(ServiceDeviceForm serviceDevice) {
		ErrorList errors = new ErrorList(serviceDevice);
		if(serviceDevice.getDeviceName().length() < 1)
			errors.addError("device_name", "Name cannot be empty");
		return errors;
	}
	
	public ErrorList findErrors(InvoiceForm invoiceForm) {
		ErrorList errors = new ErrorList(invoiceForm);
		if(invoiceForm.getReceiver_name().length() < 1)
			errors.addError("receiver_name", "Name cannot be empty");
		return errors;
	}
	
	public Integer getValidInt(String id) {
		try {
			return Integer.parseInt(id);
		} catch(Exception e) {
			return null;
		}
	}
	
}
